package com.ftrend.printdemo.mhttemplate;

import android.content.Context;
import android.util.Log;

import com.ftrend.printdemo.common.PrintBarCodeLable;
import com.ftrend.printdemo.common.PrinterBarUsb;
import com.ftrend.printdemo.common.PrinterBlue;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class PrinterCommon {




    /**
     * 打印标签
     *
     * @param page 标签页面
     * @param gap  纸张间隔
     * @throws IOException
     */
    public static void print(PrinterPaper page, float gap) throws IOException {
        Log.e("PrinterPaper","4");
        Size size = page.getSize();
        try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
            stream.write(String.format(Locale.getDefault(), "SIZE %d mm,%d mm", size.getWidth(), size.getHeight()).getBytes());
            stream.write(tsplEnterCommand());
            stream.write(String.format(Locale.getDefault(), "SIZE %d mm,%d mm", size.getWidth(), size.getHeight()).getBytes());
            stream.write(tsplEnterCommand());
            stream.write(String.format(Locale.getDefault(), "GAP %f mm,0 mm", gap).getBytes());
            stream.write(tsplEnterCommand());
            stream.write("CLS".getBytes());
            stream.write(tsplEnterCommand());
            // 适配美恒通倒着打印的问题
            if (true) {
                stream.write("DIRECTION 1 0".getBytes());
            } else {
                stream.write("DIRECTION 0 0".getBytes());
            }
            stream.write(tsplEnterCommand());
            stream.write(String.format(Locale.getDefault(), "BITMAP 0,0,%d,%d,0,", size.getWidth(), size.getHeight() * 8).getBytes());
            List<Byte> data = page.getPrintData();
            for (Byte b : data) {
                stream.write(b);
            }
            stream.write(tsplEnterCommand());
            stream.write(String.format(Locale.getDefault(), "PRINT %d", page.getPrintNum()).getBytes());
            stream.write(tsplEnterCommand());
            byte[] datas = stream.toByteArray();
            Log.e("PrinterPaper","5");
            if (PrinterBarUsb.isBarConnect())
            {
                try {
                    Log.e("PrinterPaper","6");
                    boolean status = PrinterBarUsb.getInstance().getPortManager().writeDataImmediately(datas);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }else if(PrinterBlue.isBarConnect())
            {
                try {
                    boolean status = PrinterBlue.getInstance().getPortManager().writeDataImmediately(datas);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static byte[] tsplEnterCommand() {
        return new byte[]{0x0d, 0x0a};
    }

    public static void toPrints(String printerName,Context context) {
        if(printerName.equals("MHT-L58G")||printerName.equals("MHT-P18L")){
            Log.e("PrinterPaper","2");
            try {
                List<PrinterPaper> papers = MTHPrintBarCodeUtils.of(context).sendLabel();
                for (PrinterPaper paper : papers) {
                    Log.e("PrinterPaper","3");
                    print(paper,1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
           new PrintBarCodeLable().sendLabel();
        }
    }
}
